﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo.Framework.YieldReturn
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }

    class Universe
    {
        private IEnumerable<Galaxy> galaxies;

        public Universe()
        {
            galaxies = new List<Galaxy>()
            {
                new Galaxy(){Id = 1, Name ="G1"},
                new Galaxy(){Id = 2, Name ="G2"},
                new Galaxy(){Id = 3, Name ="G3"},
            };
        }

        /* You use a yield return statement to return each element one at a time.
         * yield keyword is a Syntactic sugar
         * You consume an iterator method by using
         * a foreach statement or LINQ query. Each iteration of the foreach loop calls the iterator method.
         * When a yield return statement is reached in the iterator method, expression is returned, and the
         * current location in code is retained. Execution is restarted from that location the next time that
         * the iterator function is called.
         * */

        public IEnumerable<Galaxy> Galaxies
        {
            get
            {
                yield return new Galaxy() { Id = 1, Name = "G1" };
                yield return new Galaxy() { Id = 1, Name = "G2" };
                yield return new Galaxy() { Id = 1, Name = "G3" };
            }
        }
    }

    class Galaxy
    {
        public int Id {get;set;}
        public string Name {get;set;}
    }
}
